package com.yfp.client.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yfp.client.domain.Client;
import com.yfp.client.domain.ClientAddress;
import com.yfp.client.domain.dto.ClientDTO;
import com.yfp.client.service.ClientAddressService;
import com.yfp.client.service.ClientService;
import com.yfp.client.util.Response;
import com.yfp.client.util.SecurityUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @Description: 用户
 * 
 * @CreateDate: 2024/6/13 19:13
 * @UpdateUser: 
 * @UpdateDate: 2024/6/13 19:13
 * @UpdateRemark: 暂无修改
 * @Version: 1.0
 */
@RestController
@RequestMapping("/client")
@Tag(name = "客户")
public class ClientController {
    @Autowired
    private ClientService clientService;
    @Autowired
    private ClientAddressService clientAddressService;

    @Operation(summary = "修改用户资料")
    @PutMapping("/edit")
    public Response edit(@RequestBody ClientDTO clientDTO) {
        Client client = new Client();
        BeanUtils.copyProperties(clientDTO, client);
        client.setId(SecurityUtils.getClientId());
        clientService.edit(client);
        return Response.success();
    }

    @GetMapping("/list")
    @Operation(summary = "获取用户列表")
    public Response<List<Client>> clientList(){
        List<Client> list = clientService.clientList();
        return Response.success(list);
    }

    @GetMapping("/info")
    @Operation(summary = "获取用户详情")
    public Response<Client> getClientInfo(){
        return Response.success(clientService.getById(SecurityUtils.getClientId()));
    }

    @Operation(summary = "查询客户地址列表")
    @GetMapping("/address")
    public Response<List<ClientAddress>> findAddress() {
        LambdaQueryWrapper<ClientAddress> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ClientAddress::getClientId, SecurityUtils.getClientId());
        List<ClientAddress> list = clientAddressService.list(wrapper);
        return Response.success(list);
    }

    @Operation(summary = "查询默认客户地址")
    @GetMapping("/findDefaultAddress")
    public Response<ClientAddress> findLastAddress() {
        return Response.success(clientAddressService.findDefaultByClientId(SecurityUtils.getClientId()));
    }

    @Operation(summary = "新增/修改用户地址")
    @PostMapping("/address")
    public Response addressAdd(@RequestBody ClientAddress clientAddress) {
        clientAddress.setCreateTime(new Date());
        clientAddress.setClientId(SecurityUtils.getClientId());
        if (clientAddress.getType() == 1){
            clientAddressService.editByClientId(clientAddress.getClientId());
        }
        if (null == clientAddress.getId()){
            clientAddressService.save(clientAddress);
        } else {
            clientAddressService.updateById(clientAddress);
        }
        return Response.success(clientAddress);
    }
    @Operation(summary = "删除客户地址")
    @DeleteMapping("/delAddress/{id}")
    public Response delAddress(@PathVariable("id") Long id) {
        clientAddressService.removeById(id);
        return Response.success();
    }
}